package com.caishi.lkx.exam.question;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.caishi.lkx.exam.model.QuestionModel;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 单选题
 */
public class SingleChoiceDataProcess extends ObjectiveQuestionDataProcess<String> {

    @Override
    protected double computedAnswerQualityLevel(QuestionModel questionModel) {
        // 答案(5)：客观题结构化有答案5分、非结构化2分、无答案0分
        if (CollUtil.isNotEmpty(questionModel.getQuOption())) {
            List<List<Map<String, Object>>> option = questionModel.getQuOption().stream()
                    .filter(e -> ObjectUtil.isNotEmpty(e.get("option")))
                    .map(e -> (List<Map<String, Object>>) e.get("content"))
                    .collect(Collectors.toList());
            if (option.stream().allMatch(AbstractQuestionDataProcess::questionDataIsNotEmpty)) {
                if (option.stream().allMatch(AbstractQuestionDataProcess::questionDataIsStandard)) {
                    return 5;
                }
                return 2;
            }
        }
        return 0;
    }

    @Override
    protected double computedRightAnswerQualityLevel(QuestionModel questionModel) {
        // 正确答案(10)：客观题结构化有答案10分，非结构化有答案3分，无答案0分
        Object rightAnswer = questionModel.getAnswer();
        List<Object> answer = questionModel.getQuOption().stream().map(e -> e.get("option")).collect(Collectors.toList());
        if (answer.contains(rightAnswer)) {
            return 10;
        }
        return 0;
    }

    @Override
    public boolean userAnswerIsRight(String rightAnswer, String userAnswer) {
        if (ObjectUtil.isEmpty(userAnswer)) return false;
        return userAnswer.equals(rightAnswer);
    }
}
